/**
 * FileName: SSOSessionDao Author:   zeroleavebaoyang Date:     2018/8/29 14:46 Description:
 */
package com.shiro.sso.demo.session;

import com.shiro.sso.demo.utils.SsoLoginStore;
import java.io.Serializable;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.ValidatingSession;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;

public class SSOSessionDao extends CachingSessionDAO {

    @Override
    protected void doUpdate(Session session) {
        if(session instanceof ValidatingSession && !((ValidatingSession)session).isValid()) {
            return;
        }
        SsoLoginStore.put(session.getId().toString(), session);
    }

    @Override
    protected void doDelete(Session session) {
        SsoLoginStore.remove(session.getId().toString());
    }

    @Override
    protected Serializable doCreate(Session session) {
        Serializable sessionId = generateSessionId(session);
        assignSessionId(session, sessionId);
        SsoLoginStore.put(sessionId.toString(), session);
        return sessionId;
    }

    @Override
    protected Session doReadSession(Serializable sessionId) {
        return SsoLoginStore.get(sessionId.toString());
    }
}
